package com.xx.library.util;

import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class FileUtil {
    
    public static String uploadFile(MultipartFile file, String uploadDir) throws IOException {
        // 创建上传目录
        File dir = new File(uploadDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        
        // 生成新的文件名
        String originalFilename = file.getOriginalFilename();
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String newFilename = UUID.randomUUID().toString() + extension;
        
        // 保存文件
        File destFile = new File(dir, newFilename);
        file.transferTo(destFile);
        
        // 返回相对路径
        return uploadDir + "/" + newFilename;
    }
} 